home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / others / ole_101.zip / SCHMOO.ZIP / OLESVR.C < prev    next >
C/C++ Source or Header  |  1992-04-13  |  14KB  |  448 lines

  1. /*
  2.  * OLESVR.C
  3.  *
  4.  * Contains all callback functions in the OLESERVERVTBL struture:
  5.  *      ServerCreate
  6.  *      ServerCreateFromTemplate
  7.  *      ServerEdit
  8.  *      ServerExecute
  9.  *      ServerExit
  10.  *      ServerOpen
  11.  *      ServerRelease
  12.  *
  13.  * Also includes the constructor function PServerAllocate.
  14.  *
  15.  * Copyright(c) Microsoft Corp. 1992 All Rights Reserved
  16.  *
  17.  */
  18.  
  19. #ifdef MAKEOLESERVER
  20.  
  21. #include <windows.h>
  22. #include <ole.h>
  23. #include "schmoo.h"
  24. #include "oleglobl.h"
  25.  
  26.  
  27.  
  28. /*
  29.  * PServerAllocate
  30.  *
  31.  * Purpose:
  32.  *  Allocates a SCHMOOSERVER structure and sets the defaults in its fields.
  33.  *  Used from application initialization.
  34.  *
  35.  * Parameters:
  36.  *  pVtblSvr        LPOLESERVERVTBL used to initialize the pvtbl field.
  37.  *
  38.  * Return Value:
  39.  *  LPSCHMOOSERVER  Pointer to the allocated structure in local memory.
  40.  *                  The hMem field will contain a handle to the structure
  41.  *                  to pass to LocalFree.
  42.  *
  43.  */
  44.  
  45. LPSCHMOOSERVER FAR PASCAL PServerAllocate(LPOLESERVERVTBL pVtblSvr)
  46.     {
  47.     HANDLE            hMem;
  48.     LPSCHMOOSERVER    pSvr;
  49.  
  50.     hMem=LocalAlloc(LPTR, CBSCHMOOSERVER);
  51.  
  52.     if (NULL==hMem)
  53.         return FALSE;
  54.  
  55.     pSvr=(LPSCHMOOSERVER)(PSTR)hMem;
  56.  
  57.     //All fields are initially NULL from LMEM_ZEROINIT.
  58.     pSvr->hMem=hMem;
  59.     pSvr->pvtbl=pVtblSvr;
  60.     pSvr->fRelease=TRUE;            //Indicate a released state.
  61.  
  62.     return pSvr;
  63.     }
  64.  
  65.  
  66.  
  67. /*
  68.  * ServerCreate
  69.  *
  70.  * Purpose:
  71.  *  Creates a new object of the class in pszClass which will be
  72.  *  embedded in a client application document whose name is in
  73.  *  pszDoc.
  74.  *
  75.  * Parameters:
  76.  *  pSvr            LPSCHMOOSERVER to structure identifying the server.
  77.  *  lhDoc           LHSERVERDOC identifying the document.
  78.  *  pszClass        LPSTR specifying the class of document to create.
  79.  *  pszDoc          LPSTR specifying the document for use in window titles.
  80.  *  ppServerDoc     LPLPOLESERVERDOC in which to store a pointer
  81.  *                  to the OLESERVERDOC structure for this new document.
  82.  *
  83.  * Return Value:
  84.  *  OLESTATUS       OLE_OK if all is well, otherwise an OLE_* error value.
  85.  */
  86.  
  87. OLESTATUS FAR PASCAL ServerCreate(LPSCHMOOSERVER pSvr, LHSERVERDOC lhDoc,
  88.                                   LPSTR pszClass, LPSTR pszDoc,
  89.                                   LPLPOLESERVERDOC ppServerDoc)
  90.     {
  91.     LPSCHMOODOC     pDoc;
  92.  
  93.     /*
  94.      * 1.   Create a document of the specified class.
  95.      * 2.   Allocate and initialize an OLESERVERDOC structure.
  96.      * 3.   Store lhDoc in the OLESERVERDOC structure.
  97.      * 4.   Store a pointer to the new OLESERVERDOC structure in ppServerDoc.
  98.      * 5.   Return OLE_OK if successful, OLE_ERROR_NEW otherwise.
  99.      */
  100.  
  101.     /*
  102.      * If this were an MDI application, then we would want to create
  103.      * a new window and a new document, including a new OLESERVERDOC
  104.      * structure.  The protocol supports this, however, this application
  105.      * is single-document.
  106.      */
  107.  
  108.     pDoc=PDocumentAllocate(&pOLE->vtblDoc);
  109.  
  110.     if ((LPSCHMOODOC)NULL==pDoc)
  111.         return OLE_ERROR_NEW;
  112.  
  113.     pSvr->pDoc=pDoc;
  114.  
  115.     pDoc->lh=lhDoc;
  116.     *ppServerDoc=(LPOLESERVERDOC)pDoc;
  117.     return OLE_OK;
  118.     }
  119.  
  120.  
  121.  
  122.  
  123. /*
  124.  * ServerCreateFromTemplate
  125.  *
  126.  * Purpose:
  127.  *  Creates a new document initialized with the data in a specified
  128.  *  file.  Thw new document is opened for editing and is embedded
  129.  *  within the client when the server is updated or closed.
  130.  *
  131.  * Parameters:
  132.  *  pSvr            LPSCHMOOSERVER to structure identifying the server.
  133.  *  lhDoc           LHSERVERDOC identifying the document.
  134.  *  pszClass        LPSTR specifying the class of document to create.
  135.  *  pszDoc          LPSTR to the permanent name of the document to open.
  136.  *  pszTemp         LPSTR to the file to use as a template.
  137.  *  ppServerDoc     LPLPOLESERVERDOC in which to store a pointer
  138.  *                  to the OLESERVERDOC structure for this new document.
  139.  *
  140.  * Return Value:
  141.  *  OLESTATUS       OLE_OK if all is well, otherwise an OLE_* error value.
  142.  */
  143.  
  144. OLESTATUS FAR PASCAL ServerCreateFromTemplate(
  145.                                 LPSCHMOOSERVER pSvr, LHSERVERDOC lhDoc,
  146.                                 LPSTR pszClass, LPSTR pszDoc, LPSTR pszTemp,
  147.                                 LPLPOLESERVERDOC ppServerDoc)
  148.     {
  149.     POLYLINE        pl;
  150.     LPSCHMOODOC     pDoc;
  151.  
  152.     /*
  153.      * 1.   Create a document of the specified class.
  154.      * 2.   Read the contents of the specified file and initialize the document.
  155.      * 3.   Allocate and initialize an OLESERVERDOC structure.
  156.      * 4.   Store lhDoc in the OLESERVERDOC structure.
  157.      * 5.   Store a pointer to the new OLESERVERDOC structure in ppServerDoc.
  158.      * 6.   Return OLE_OK if successful, OLE_ERROR_TEMPLATE otherwise.
  159.      */
  160.  
  161.     //pszTemp contains a filename.
  162.     if (!FMooFileRead(pGlob, pszTemp, &pl))
  163.         return OLE_ERROR_TEMPLATE;
  164.  
  165.     pDoc=PDocumentAllocate(&pOLE->vtblDoc);
  166.  
  167.     if ((LPSCHMOODOC)NULL==pDoc)
  168.         return OLE_ERROR_TEMPLATE;
  169.  
  170.     pSvr->pDoc=pDoc;
  171.  
  172.     pDoc->lh=lhDoc;
  173.     *ppServerDoc=(LPOLESERVERDOC)pDoc;
  174.  
  175.     SendMessage(pGlob->hWndPolyline, PLM_POLYLINESET, TRUE, (LONG)(LPSTR)&pl);
  176.  
  177.     //The titles in this window will be set later through SetHostNames.
  178.     pGlob->fOpenFile=TRUE;
  179.     return OLE_OK;
  180.     }
  181.  
  182.  
  183.  
  184.  
  185. /*
  186.  * ServerEdit
  187.  *
  188.  * Purpose:
  189.  *  Creates a document initialized with data from a subsequent call
  190.  *  to the SetData function.  The object is embedded in the client
  191.  *  application and the server is not visible.
  192.  *
  193.  * Parameters:
  194.  *  pSvr            LPSCHMOOSERVER to structure identifying the server.
  195.  *  lhDoc           LHSERVERDOC identifying the document.
  196.  *  pszClass        LPSTR specifying the class of document to create.
  197.  *  pszDoc          LPSTR specifying the document for use in window titles.
  198.  *  ppServerDoc     LPLPOLESERVERDOC in which to store a pointer
  199.  *                  to the OLESERVERDOC structure for this new document.
  200.  *
  201.  * Return Value:
  202.  *  OLESTATUS       OLE_OK if all is well, otherwise an OLE_* error value.
  203.  */
  204.  
  205. OLESTATUS FAR PASCAL ServerEdit(LPSCHMOOSERVER pSvr, LHSERVERDOC lhDoc,
  206.                                 LPSTR pszClass, LPSTR pszDoc,
  207.                                 LPLPOLESERVERDOC ppServerDoc)
  208.     {
  209.     LPSCHMOODOC     pDoc;
  210.  
  211.     /*
  212.      * This is the same as Open, but tells the server to expect
  213.      * a SetData call and that the server is initially hidden.
  214.      *
  215.      * 1.   Create a document of the specified class.
  216.      * 2.   Allocate and initialize an OLESERVERDOC structure.
  217.      * 3.   Store lhDoc in the OLESERVERDOC structure.
  218.      * 4.   Store pointer to the new OLESERVERDOC structure in ppServerDoc.
  219.      * 5.   Return OLE_OK if successful, OLE_ERROR_EDIT otherwise.
  220.      */
  221.  
  222.     pDoc=PDocumentAllocate(&pOLE->vtblDoc);
  223.  
  224.     if ((LPSCHMOODOC)NULL==pDoc)
  225.         return OLE_ERROR_EDIT;
  226.  
  227.     pSvr->pDoc=pDoc;
  228.  
  229.     pDoc->lh=lhDoc;
  230.     *ppServerDoc=(LPOLESERVERDOC)pDoc;
  231.  
  232.     return OLE_OK;
  233.     }
  234.  
  235.  
  236.  
  237.  
  238. /*
  239.  * ServerExecute
  240.  *
  241.  * Purpose:
  242.  *  Handles DDE Execute commands sent from the server library
  243.  *  from the client application.  Not all applications need to support
  244.  *  DDE Execute.
  245.  *
  246.  * Parameters:
  247.  *  pSvr            LPSCHMOOSERVER identifying the server closing.
  248.  *  hCommands       HANDLE to global memory containing the DDE execute
  249.  *                  string.
  250.  *
  251.  * Return Value:
  252.  *  OLESTATUS       OLE_OK if all is well, otherwise an OLE_* error value.
  253.  *                  This functions returns OLE_ERROR_COMMAND to indicate
  254.  *                  that it is not implemented.
  255.  */
  256.  
  257. OLESTATUS FAR PASCAL ServerExecute(LPSCHMOOSERVER pSvr, HANDLE hCommands)
  258.     {
  259.     /*
  260.      * 1.   Lock the hCommands handle to access the execute strings.
  261.      * 2.   Parse and execute the commands as necessary.
  262.      * 3.   DO NOT FREE hCommands.
  263.      * 4.   Return OLE_OK if successful, OLE_ERROR_COMMAND otherwise.
  264.      */
  265.  
  266.     return OLE_ERROR_COMMAND;
  267.     }
  268.  
  269.  
  270.  
  271.  
  272.  
  273. /*
  274.  * ServerExit
  275.  *
  276.  * Purpose:
  277.  *  Instructs the server application to close documents and shut down.
  278.  *
  279.  * Parameters:
  280.  *  pSvr            LPSCHMOOSERVER identifying the server closing.
  281.  *
  282.  * Return Value:
  283.  *  OLESTATUS       OLE_OK if all is well, otherwise an OLE_* error value.
  284.  */
  285.  
  286. OLESTATUS FAR PASCAL ServerExit(LPSCHMOOSERVER pSvr)
  287.     {
  288.  
  289.     /*
  290.      * 1.   Hide the window to prevent any user interaction.
  291.      * 2.   Call OleRevokeServer.  Ignore an OLE_WAIT_FOR_RELEASE return value.
  292.      * 3.   Perform whatever action is necessary to cause the application
  293.      *      to terminate, such as DestroyWindow.
  294.      * 4.   Return OLE_OK if successful, OLE_ERROR_GENERIC otherwise.
  295.      */
  296.  
  297.     ShowWindow(pGlob->hWnd, SW_HIDE);
  298.  
  299.     pSvr->fRelease=FALSE;
  300.     OleRevokeServer(pSvr->lh);
  301.  
  302.     DestroyWindow(pGlob->hWnd);
  303.     return OLE_OK;
  304.     }
  305.  
  306.  
  307.  
  308.  
  309.  
  310. /*
  311.  * ServerOpen
  312.  *
  313.  * Purpose:
  314.  *  Opens an exiting file (document) and prepares the document for
  315.  *  editing, generally done when a user double-clicks a linked
  316.  *  object in the client.  Note that the server is hidden at this
  317.  *  point.
  318.  *
  319.  * Parameters:
  320.  *  pSvr            LPSCHMOOSERVER to structure identifying the server.
  321.  *  lhDoc           LHSERVERDOC identifying the document.
  322.  *  pszDoc          LPSTR to the permanent name of the document to
  323.  *                  be opened.
  324.  *  ppServerDoc     LPLPOLESERVERDOC in which to store a pointer
  325.  *                  to the OLESERVERDOC structure for this new document.
  326.  *
  327.  * Return Value:
  328.  *  OLESTATUS       OLE_OK if all is well, otherwise an OLE_* error value.
  329.  */
  330.  
  331. OLESTATUS FAR PASCAL ServerOpen(LPSCHMOOSERVER pSvr, LHSERVERDOC lhDoc,
  332.                                 LPSTR pszDoc, LPLPOLESERVERDOC ppServerDoc)
  333.     {
  334.     POLYLINE        pl;
  335.     LPSCHMOODOC     pDoc;
  336.  
  337.     /*
  338.      * 1.   Create a document of the specified class.
  339.      * 2.   Read the contents of the specified file and initialize the document.
  340.      * 3.   Save the filename of the loaded file with this document,
  341.      *      if necessary.
  342.      * 4.   Allocate and initialize an OLESERVERDOC structure.
  343.      * 5.   Store lhDoc in the OLESERVERDOC structure.
  344.      * 6.   Store a pointer to the new OLESERVERDOC structure in ppServerDoc.
  345.      * 7.   Return OLE_OK if successful, OLE_ERROR_OPEN otherwise.
  346.      */
  347.  
  348.     if (!FMooFileRead(pGlob, pszDoc, &pl))
  349.         return OLE_ERROR_OPEN;
  350.  
  351.     pDoc=PDocumentAllocate(&pOLE->vtblDoc);
  352.  
  353.     if ((LPSCHMOODOC)NULL==pDoc)
  354.         return OLE_ERROR_OPEN;
  355.  
  356.     //Save the document in the server for later access in ServerRelease.
  357.     pSvr->pDoc=pDoc;
  358.  
  359.     pDoc->lh=lhDoc;
  360.     *ppServerDoc=(LPOLESERVERDOC)pDoc;
  361.  
  362.     WindowTitleSet(pGlob->hWnd, pszDoc);
  363.     SendMessage(pGlob->hWndPolyline, PLM_POLYLINESET, TRUE, (LONG)(LPSTR)&pl);
  364.     pGlob->fOpenFile=TRUE;
  365.  
  366.     return OLE_OK;
  367.     }
  368.  
  369.  
  370.  
  371.  
  372.  
  373. /*
  374.  * ServerRelease
  375.  *
  376.  * Purpose:
  377.  *  Notifies a server that all connections to it have been closed and
  378.  *  that the server application can terminate.  This function should
  379.  *  change the state of a global flag that causes any PeekMessage
  380.  *  waiting loop to exit.
  381.  *
  382.  * Parameters:
  383.  *  pSvr            LPSCHMOOSERVER identifying the server.
  384.  *
  385.  * Return Value:
  386.  *  OLESTATUS       OLE_OK if all is well, otherwise an OLE_* error value.
  387.  */
  388.  
  389. OLESTATUS FAR PASCAL ServerRelease(LPSCHMOOSERVER pSvr)
  390.     {
  391.     /*
  392.      * 1.   Set a flag to indicate that Release has been called.
  393.      * 2.   If the application is hidden, we must use ServerRelease to
  394.      *      instruct the application to terminate, by posting a WM_CLOSE
  395.      *      or otherwise effective message, then exit the method with OLE_OK.
  396.      * 3.   Free any resources allocated for this server, like documents,
  397.      *      but DO NOT free the SCHMOOSERVER structure itself.
  398.      * 4.   Return OLE_OK if successful, OLE_ERROR_GENERIC otherwise.
  399.      */
  400.  
  401.     pSvr->fRelease=TRUE;
  402.  
  403.     /*
  404.      * If we are invisible when we get ServerRelease, that's a flag
  405.      * to us meaning exit.  Posting a WM_CLOSE takes care of all
  406.      * this.  Note that even though WM_CLOSE processing in SCHMOO.C
  407.      * checks for a dirty file and asks to save if necessary, the
  408.      * file cannot be dirty because there has been no chance for
  409.      * the user to make any changes.
  410.      *
  411.      * ServerRelease may be called twice when the server is opened invisible
  412.      * for an update of a client object.  In this case, we'll get
  413.      * ServerRelease once, where we should post a message to terminate the
  414.      * application.  The second time around we need to handle free resources
  415.      * associated with the server.  We detect this through the validity
  416.      * pOLE->pSvr->lh which we set to 0L in FFileExit before calling
  417.      * OleRevokeServer.  0L in lh signals us that we're in the final
  418.      * revoke and we can free documents.
  419.      *
  420.      */
  421.     if (!IsWindowVisible(pGlob->hWnd) && 0L!=pOLE->pSvr->lh)
  422.         {
  423.         PostMessage(pGlob->hWnd, WM_CLOSE, 0, 0L);
  424.         return OLE_OK;
  425.         }
  426.  
  427.     /*
  428.      * Free the document if we are closing from OleRevokeServer, not through
  429.      * a client releasing the server.
  430.      */
  431.     if (0L==pOLE->pSvr->lh)
  432.         {
  433.         //Free the document we're holding.
  434.         if (NULL!=pSvr->pDoc)
  435.             {
  436.             if (NULL!=pSvr->pDoc->hMem)
  437.                 LocalFree(pSvr->pDoc->hMem);
  438.             }
  439.  
  440.         pSvr->pDoc=NULL;
  441.         }
  442.  
  443.     return OLE_OK;
  444.     }
  445.  
  446.  
  447. #endif //MAKEOLESERVER
  448.